package com.hudongwx.sadpw.fragment;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hudongwx.sadpw.R;
import com.hudongwx.sadpw.fragment.community.PZSHFragment;
import com.hudongwx.sadpw.fragment.community.TuiJianFragment;
import com.hudongwx.sadpw.fragment.community.WLSDFragment;
import com.hudongwx.sadpw.fragment.community.YDJSFragment;
import com.hudongwx.sadpw.fragment.community.YSYSFragment;

import org.xutils.view.annotation.ContentView;
import org.xutils.x;

/**
 * A simple {@link Fragment} subclass.
 */
@ContentView(R.layout.fragment_community)
public class CommunityFragment extends Fragment {
    private View view;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private TuiJianFragment tuijianFragment;
    private PZSHFragment pzshFragment;
    private WLSDFragment wlsdFragment;
    private  YDJSFragment ydjsFragment;
    private YSYSFragment ysysFragment;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       if (null==view){
           view= x.view().inject(this,inflater,container);
           tabLayout= (TabLayout) view.findViewById(R.id.community_tablayout);
           viewPager= (ViewPager) view.findViewById(R.id.community_viewpager);
           Myadapter adapter=new Myadapter(getActivity().getSupportFragmentManager());
           viewPager.setAdapter(adapter);
           //设置tablayout和viewpager联动
           tabLayout.setupWithViewPager(viewPager);
       }
        return view;
    }

    class Myadapter extends FragmentStatePagerAdapter {
        public Myadapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    if (tuijianFragment==null){
                        tuijianFragment=new TuiJianFragment();
                    }
                    return tuijianFragment;
                case 1:
                    if (wlsdFragment==null){
                        wlsdFragment=new WLSDFragment();
                    }
                    return wlsdFragment;
                case 2:
                    if(ysysFragment==null){
                        ysysFragment=new YSYSFragment();
                    }
                    return ysysFragment;
                case 3:
                    if (ydjsFragment==null){
                        ydjsFragment=new YDJSFragment();
                    }
                    return ydjsFragment;
                case 4:
                    if (pzshFragment==null){
                        pzshFragment=new PZSHFragment();
                    }
                    return pzshFragment;
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return 5;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position){
                case 0:
                    return "推荐";
                case 1:
                    return "我来晒单";
                case 2:
                    return "饮食养生";
                case 3:
                    return "运动健康";
                case 4:
                    return "品质生活";
                default:
                    return null;
            }
        }
    }
}
